package web.eshop.panels;

import application.MySession;
import domain.User;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.user.UserService;
import web.commons.LogOffLink;
import web.eshop.order.OrderPage;
import web.login.UserSettingsPage;

/**
 * Little panel with cart(order) information.
 */
public class ShoppingCartPanel extends ShopBasePanel {

    @SpringBean
    private UserService userService;

    @SpringBean(name = "mySession")
    private MySession mySession;

    public ShoppingCartPanel(String id) {
        super(id);
        MarkupContainer kosik = new WebMarkupContainer("kosik");
        add(kosik);
        IModel<User> userIModel = new LoadableDetachableModel<User>() {
            @Override
            protected User load() {
                return userService.findUserById(mySession.getId());
            }
        };


        Link orderPage = new BookmarkablePageLink("orderPage", OrderPage.class);
        kosik.add(orderPage);
        Link logoff = new LogOffLink("logoff");
        kosik.add(logoff);
        Link userSettingsLink = new BookmarkablePageLink("UserSettingsPage", UserSettingsPage.class);
        userSettingsLink.add(new Label("firstName", new Model(userIModel.getObject().getFirstName())));
        userSettingsLink.add(new Label("lastName", new Model(userIModel.getObject().getLastName())));
        userSettingsLink.add(new Label("login", new Model(userIModel.getObject().getLogin())));

        kosik.add(userSettingsLink);


        LoadableDetachableModel<Integer> countModel = new LoadableDetachableModel<Integer>() {

            @Override
            protected Integer load() {
                return mySession.getDifferentItemCountInBasket();
            }
        };
        LoadableDetachableModel<String> priceModel = new LoadableDetachableModel<String>() {

            @Override
            protected String load() {
                return mySession.getTotalPriceAsString();
            }
        };

        kosik.add(new Label("price", priceModel));
        kosik.add(new Label("numberOfItems", countModel));
    }
}
